package com.android.porttallymansystem.ui.activity

import android.app.Activity
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import android.opengl.Visibility
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.core.content.FileProvider
import com.android.porttallymansystem.R
import com.android.porttallymansystem.databinding.ActivityWorkBinding
import com.android.porttallymansystem.logic.Repository
import java.io.File
import kotlin.concurrent.thread

class WorkActivity : AppCompatActivity() {

    private lateinit var binding: ActivityWorkBinding

    private var workId: Long? = null

    private lateinit var outputImage: File
    private lateinit var imageUri: Uri
    private val takePhoto = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityWorkBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setSupportActionBar(binding.toolbarWork)

        setStatusBar()

        initData()

        setListener()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun setListener() {
        binding.btWork.setOnClickListener {
            // 创建File对象， 用于存储拍照后的照片
            outputImage = File(externalCacheDir, "${binding.tvWorkTitle.text}.jpg")
            if (outputImage.exists()) {
                outputImage.delete()
            }
            outputImage.createNewFile()
            imageUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                FileProvider.getUriForFile(this, "com.android.porttallymansystem.fileprovider", outputImage)
            } else {
                Uri.fromFile(outputImage)
            }
            // 启动相机程序
            val intent = Intent("android.media.action.IMAGE_CAPTURE")
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
            startActivityForResult(intent, takePhoto)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            takePhoto -> {
                if (resultCode == Activity.RESULT_OK) {
                    val bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(imageUri))
                    binding.ivWork.visibility = View.VISIBLE
                    binding.ivWork.setImageBitmap(bitmap)

                    thread {
                        Repository.finishWork(workId!!)
                    }

                    binding.tvWorkIsFinished.text = "已完成"
                    binding.tvWorkIsFinished.setTextColor(resources.getColor(R.color.gray))
                }
            }
        }
    }

    private fun initData() {
        workId = intent.getLongExtra("workId", -1)
        thread {
            val work = Repository.getWorkById(workId!!)
            runOnUiThread {
                binding.tvWorkDeadline.text = String.format(
                    resources.getString(R.string.time_2),
                    work.yearOfDeadLine,
                    work.monthOfDeadLine,
                    work.dayOfDeadLine
                )
                binding.tvWorkTitle.text = work.title
                binding.tvWorkContent.text = work.content
                binding.tvWorkIsFinished.text = if (work.isFinished) {
                    binding.btWork.visibility = View.GONE
                    binding.tvWorkIsFinished.setTextColor(resources.getColor(R.color.gray))
                    "已完成"
                } else {
                    binding.tvWorkIsFinished.setTextColor(resources.getColor(R.color.red))
                    "未完成"
                }
            }
        }
    }

    private fun setStatusBar() {
//        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
//                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        window.statusBarColor = resources.getColor(R.color.green)
    }
}